//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    info: {
      avatar:'',
      name:'微信用户',
      votes:0,
      jifen:0,
    },
    rainArr: [], // 雨滴值 点击收取
    guoshuArr:[],
    isguoshu:false,
    current:0,
    stages: 1, // 成长阶段 1(小树[默认])，中2(中树) ，大3(大树)
    during: 0, // 阶段阈值 1、小树[100以下](during > votes ) ，2、中树[100及以上 并且小于1000](during <= votes && oldest > votes)
    oldest: 0, // 阶段阈值 3、大树[1000及以上](oldest <= votes )
    plussNum: 1, // 加值数量（默认1
    plusNum:0,
    shuiNum:0,
    myguoshu:null,
    isguoshuxianshi:false,
    pluss: false, // 水滴值+1动画开关
    movetree: true, // 树动画开关
    treemove: false, // 树大小动画类型开关
    wateroff: true, // 浇水动画开关
    watercss: false, // 水壶动画开关
    waterdom: false, // 水滴动画开关
    tmnum:[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27]
  },
  
  //事件处理函数
  bindViewTap: function () {
  },
  bindReplaceInput:function(e){
    this.getuusernickname(e.detail.value)
  },
  create:function(){

  },
  onLoad: function() {
    if (app.globalData.userInfo) {
      this.data.info.avatar  = app.globalData.userInfo.imguri
      this.data.info.name  = app.globalData.userInfo.nickName
      this.data.info.votes  = 0
      this.data.info.jifen  = app.globalData.userInfo.jifen
      this.data.shuiNum  = app.globalData.userInfo.shuinum
      this.setData({
        info:this.data.info
      })
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      wx.login({
        success: res => {
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          wx.request({
            url: 'https://api.weixin.qq.com/sns/jscode2session',
            method:'GET',
            data:{
              appid:'wxafe8c7df048a54c9',
              secret:'47f5c530c9560baa913d45a23f2437a6',
              js_code:res.code,
              grant_type:'authorization_code'
            },
            success:res=>{
              wx.request({
                url: 'http://127.0.0.1:8000/getuserinfo',
                method:'GET',
                data:{
                  wxid:res.data.openid,
                },
                success:(res)=>{
                  app.globalData.userInfo = res.data
                  this.data.info.avatar  = app.globalData.userInfo.imguri
                  this.data.info.name  = app.globalData.userInfo.nickName
                  this.data.info.votes  = 0
                  this.data.info.jifen  = app.globalData.userInfo.jifen
                  this.data.shuiNum  = app.globalData.userInfo.shuinum
                  this.setData({
                    info:this.data.info,
                    shuiNum:this.data.shuiNum
                  })
                  this.setData({
                    info:this.data.info
                  })
                  this.setData({
                    userInfo: app.globalData.userInfo,
                    hasUserInfo: true
                  })
                  wx.request({
                    url: 'http://127.0.0.1:8000/guoshumy',
                    method:'GET',
                    data:{
                      wxid:app.globalData.userInfo.wxid,
                    },
                    success:(res)=>{
                      console.log(res)
                      this.data.myguoshu=res.data.data
                      if(res.data.data!=null && res.data.guoshu!=null){
                        this.data.isguoshuxianshi = true
                        this.data.during = res.data.guoshu.position
                        this.data.oldest = res.data.guoshu.is_img
                        if(res.data.data.stages>=res.data.guoshu.position && res.data.data.stages<res.data.guoshu.is_img){
                          this.data.stages = 2
                        }else if(res.data.data.stages<res.data.guoshu.position){
                          this.data.stages = 1
                        }else if(res.data.data.stages>=res.data.guoshu.is_img){
                          this.data.stages = 3
                        }
                        this.data.rainArr.push(res.data.guoshu.jfnum)
                        this.setData({
                          isguoshuxianshi:true,
                          myguoshu:res.data.data,
                          plusNum:res.data.data.stages,
                          during:res.data.guoshu.position,
                          oldest:res.data.guoshu.is_img,
                          stages:this.data.stages,
                          rainArr:this.data.rainArr
                        })
                      }
                    },
                  })
                },
              })
              
            },
          })
          wx.request({
            url: 'http://127.0.0.1:8000/guoshu',
            method:'GET',
            success:(res)=>{
              this.data.guoshuArr = res.data.data
              this.setData({
                guoshuArr:res.data.data
              })
            },
          })
        }
      })
    }
  },
  // 竞猜函数
  jingcai:function(){
    wx.showModal({
      title: '竞猜',
      content: '',
      placeholderText:'竞猜维护后的开奖结果',
      editable:true,
      showCancel:true,
      confirmColor:'#576B95',
      confirmText:'提交竞猜',
      success:res=>{
       if (res.confirm) {
         let tm  = parseInt(res.content)
         if(this.data.tmnum.includes(tm)){
           // 包含
           wx.request({
            url: 'http://127.0.0.1:8000/jingcais',
            method:'GET',
            data:{
              wxid:app.globalData.userInfo.wxid,
              jingcai:tm
            },
            success:(res)=>{
              if(res.data.code == 200){
                console.log(res)
                wx.showToast({
                  title: res.data.msg,
                  icon:'success'
                })
              }else{
                wx.showToast({
                  title: res.data.msg,
                  icon:'error'
                })
              }
            },
          })
         }else{
           // 不包含
           wx.showToast({
             title: '竞猜内容错误',
             icon:'error'
           })
         }
      } else if (res.cancel) {
        console.log('用户点击取消')
      }
      },
      fail:res=>{
        console.log(456) 
       }
    })
  },
  // 确认选择果树
  guoshubj:function(){
    this.data.isguoshu = false
    this.setData({
      isguoshu:false
    })
  },
  guoshu:function(){
    this.data.isguoshu = true
    this.setData({
      isguoshu:true
    })
  },
  guoshucurrent:function(event){
    console.log(event)
    this.data.current = event.detail.current
  },
  querenguoshu:function(){
    this.data.isguoshu = false
    this.setData({
      isguoshu:false,
      current:this.data.current
    })
    let guoshuid = this.data.guoshuArr[this.data.current].id
    wx.request({
      url: 'http://127.0.0.1:8000/guoshus',
      method:'GET',
      data:{
        wxid:app.globalData.userInfo.wxid,
        guoshuid:guoshuid
      },
      success:res=>{
        console.log(res)
        if(res.data.code == 200){
          // 创建果树成功
          this.data.stages = 1
          this.data.during = this.data.guoshuArr[this.data.current].position
          this.data.oldest = this.data.guoshuArr[this.data.current].is_img
          this.data.rainArr.push(res.data.gsdata.jfnum)
          this.setData({
            stages:1,
            during: this.data.guoshuArr[this.data.current].position,
            oldest:this.data.guoshuArr[this.data.current].is_img,
            rainArr:this.data.rainArr
          })
          this.isguoshuxianshi = true
          this.setData({
            isguoshuxianshi:true
          })
        }else{
          // 创建果树失败
          wx.showToast({
            title: '有果树未成熟',
            icon:'error'
          })
        }
      }
    })
  },
  // 签到函数
  getqiandao:function(){
    wx.request({
      url: 'http://127.0.0.1:8000/qiandao',
      method:'GET',
      data:{
        wxid:app.globalData.userInfo.wxid
      },
      success:(res)=>{
        if(res.data.code == 200){
          app.globalData.userInfo = res.data.data
          this.data.info.avatar  = app.globalData.userInfo.imguri
          this.data.info.name  = app.globalData.userInfo.nickName
          this.data.info.votes  = 0
          this.data.info.jifen  = app.globalData.userInfo.jifen
          this.data.shuiNum  = app.globalData.userInfo.shuinum
          this.setData({
            info:this.data.info,
            shuiNum:this.data.shuiNum
          })
          this.setData({
            info:this.data.info
          })
          this.setData({
            userInfo: app.globalData.userInfo,
            hasUserInfo: true
          })
          wx.showToast({
            title: res.data.msg,
            icon:'success'
          })
        }else{
          wx.showToast({
            title: res.data.msg,
            icon:'error'
          })
        }
      },
    })
  },
  getuusernickname:function(nickname){
    wx.request({
      url: 'http://127.0.0.1:8000/uusernickname',
      method:'GET',
      data:{
        wxid:app.globalData.userInfo.wxid,
        nickName:nickname,
      },
      success:(res)=>{
        if(res.data.code == 200){
          this.data.info.name  = nickname
          app.globalData.userInfo.nickName= nickname
          this.setData({
            info:this.data.info,
            userInfo:app.globalData.userInfo
          })
        }
      },
      fail:function(err){
        console.log(err)
      }
    })
  },
  getUserInfo: function (e) {
    app.globalData.userInfo.imguri = e.detail.avatarUrl
    wx.request({
      url: 'http://127.0.0.1:8000/uuserimgurl',
      method:'GET',
      data:{
        wxid:app.globalData.userInfo.wxid,
        imgurl:e.detail.avatarUrl,
      },
      success:(res)=>{
        if(res.data.code == 200){
          let info = this.data.info;
          info.avatar = app.globalData.userInfo.imguri;
          info.name = this.data.info.name
          this.setData({
            userInfo: app.globalData.userInfo,
            info:info,
            hasUserInfo: true
          })
        }
      },
      fail:function(err){
        console.log(err)
      }
    })
  },

  // 设置树的大小，恢复动画
  setTree(time = 4000) {
    setTimeout(() => {
      this.setData({
        pluss: false,
        wateroff: true,
        watercss: false,
        treemove: false
      });
      // 小树阶段
      console.log(this.data.info)
      if (this.data.during > this.data.plusNum) {
        this.setData({
          stages: 1
        });
      }
      // 中树阶段
      if (this.data.during <= this.data.plusNum && this.data.oldest > this.data.plusNum) {
        this.setData({
          stages: 2
        });
      }
      // 大树阶段
      if (this.data.oldest <= this.data.plusNum) {
        this.setData({
          stages: 3
        });
      }
    }, time);
  },

  // 收取雨滴的动画
  rainFun(e) {
    let { index } = e.currentTarget.dataset;
    wx.request({
      url: 'http://127.0.0.1:8000/shouqu',
      method:'GET',
      data:{
        wxid:app.globalData.userInfo.wxid,
        jf:this.data.rainArr[0]
      },
      success:res=>{
        if(res.data.code == 200){
          wx.showToast({
            title: '收取成功',
            icon:'success'
          })
          this.data.isguoshuxianshi = false
          this.data.plusNum = 0
          this.data.oldest = 0
          this.data.info.jifen = this.data.info.jifen+this.data.rainArr[0]
          this.data.rainArr.splice(index, 1);
          setTimeout(() => {
            this.setData({
              rainArr:this.data.rainArr,
              isguoshuxianshi:this.data.isguoshuxianshi,
              plusNum:0,
              oldest:0,
              info:this.data.info
            });
          }, 1000);
          this.setTree(2000);
         
        }
      }
    })
  },

  // 点击树的动画
  tree() {
    if (this.data.movetree) {
      this.setData({
        treemove: true,
        movetree: false
      });
      setTimeout(() => {
        this.setData({  
          movetree: true,
          treemove: false
        });
      }, 1000);
    };
  },

  // 水壶浇水动画
  water() {
    if (this.data.wateroff) {
      if(this.data.shuiNum<=0){
        return false;
      }
      if(!this.data.isguoshuxianshi){
        return false
      }
      if(this.data.plusNum>=this.data.oldest){
        wx.showToast({
          title: '请收取果树积分',
        })
        return false
      }
      let shuiNum = 0
      if(this.data.shuiNum+this.data.plusNum>this.data.oldest){
        shuiNum = this.data.oldest - this.data.plusNum
      }else{
        shuiNum =this.data.shuiNum
      }
      wx.request({
        url: 'http://127.0.0.1:8000/jiaoshui',
        method:'GET',
        data:{
          wxid:app.globalData.userInfo.wxid,
          shuinum:shuiNum
        },
        success:res=>{
          console.log(res)
        }
      })
      this.setData({
        watercss: true,
        wateroff: false
      });
      setTimeout(() => {
        this.setData({
          waterdom: true
        });
      }, 1500);
      setTimeout(() => {
        let info = this.data.info;
        info.votes = Number(this.data.info.votes) + shuiNum;
        this.data.plusNum = Number(this.data.plusNum) + shuiNum;
        this.data.shuiNum = Number(this.data.shuiNum) - shuiNum
        this.setData({
          info:info,
          shuiNum:this.data.shuiNum,
          plusNum:this.data.plusNum,
          pluss: true,
          treemove: true,
          movetree: false
        });
      }, 2000);
      setTimeout(() => {
        this.setData({
          waterdom: false,
          movetree: true
        })
      }, 3500);
      this.setTree();
    };
  },
})